package com.lcx.movie.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.Cinema;
import com.lcx.movie.service.CinemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李丽君
 * @version 1.0
 * @Description 影院控制类
 * @date 2025/2/11 15:31
 */
@CrossOrigin(origins = "*")
@RestController
public class CinemaController {
    @Autowired
    private CinemaService cinemaService;
    /**
     * 根据邮箱判断管理员属于哪个影院
     * @param userEmail 用户邮箱
     * @return 影院集合
     */
    @GetMapping("/selectCinemaByEmail")
    public Rep selectCinemaByEmail(String userEmail){
        Cinema cinemaIds = cinemaService.selectCinemaByEmail(userEmail);
        return new Rep().addData("cinemaIds", cinemaIds);
    }

    /**
     * 分页查看平台影院管理 （影院名称）
     * @param page 当前页码
     * @param cinemaName 影院名
     * @return Rep 对象
     */
    @GetMapping("selectCinema/{page}")
    public Rep selectCinemaPage(@PathVariable("page")int page, @RequestParam(required = false) String cinemaName) {
        PageHelper.startPage(page,10);
        //执行查询
        List<Cinema>list=this.cinemaService.selectCinemaPage(cinemaName);
        PageInfo <Cinema>cinemaPageInfo=new PageInfo<>(list);
        return new Rep().addData("cinemaPageInfo",cinemaPageInfo);
    }
    /**
     * 平台新增影院
     * @param cinema 影院实体
     * @return Rep对象
     */
    @PostMapping("/insertCinema")
    public Rep insertCinema(@RequestBody Cinema cinema) {
        Rep rep=new Rep();
        boolean save = cinemaService.save(cinema);
        if (save){
            Long cinemaId = cinema.getCinemaId();
            System.out.println("cinemaId =============== " + cinemaId);
            return rep.setMessage("success").addData("cinemaId",cinema.getCinemaId());
        }else{
            return rep.setMessage("fail");
        }

    }

    /**
     * 平台修改影院
     * @param cinema 影院对象
     * @return 字符串消息
     */
    @PostMapping("/updateCinema")
    public String updateCinema(@RequestBody Cinema cinema) {
        return cinemaService.updateCinema(cinema);
    }
    /**
     * 平台根据id删除影院
     * @param cinemaId
     * @return
     */
    @GetMapping("/deleteCinemaId")
    public String deleteCinema(Integer cinemaId) {
        return cinemaService.deleteCinema(cinemaId);
    }


    /**
     * 平台管理员新增电影时，查询所有的影院
     * @return 影院集合
     */
    @GetMapping("/getCinemas")
    public Rep getCinemas(){
        List<Cinema> cinemas = cinemaService.getCinemas();
        return new Rep().addData("cinemas",cinemas);
    }

}